<template>

	<view class="container">

		<!-- 左上角X号按钮 -->
		<view class="close-btn" @click="handleClose">
			<text class="x-icon">×</text>
		</view>
		<view class="title">用手机号注册</view>
		<view class="avatar-box" @click="chooseAvatar">
			<image v-if="avatarUrl" :src="avatarUrl" class="avatar-img" />
			<view v-else class="avatar-placeholder">
				<text class="camera-icon">📷</text>
			</view>
		</view>
		<view class="form-box">
			<view class="form-row">
				<view class="label">昵称</view>
				<input class="input" v-model="nickname" placeholder="请填写昵称" placeholder-class="input-placeholder" />
			</view>
			<view class="form-row">
				<view class="label">国家/地区</view>
				<view class="country">中国大陆 <text class="country-code">(+86)</text></view>
			</view>
			<view class="form-row">
				<view class="label">手机号</view>
				<input class="input" v-model="phone" type="number" placeholder="请填写手机号码"
					placeholder-class="input-placeholder" />
			</view>
			<view class="form-row">
				<view class="label">密码</view>
				<input class="input" v-model="password" type="password" placeholder="请设置密码"
					placeholder-class="input-placeholder" />
			</view>
		</view>
		<view class="button-row">
			<view class="agreement-row">
				<radio shape="circle" :checked="agreed" @click="toggleAgree" class="checkbox" />
				<text class="agreement-text">我已阅读并同意 <text class="agreement-link">《软件许可及服务协议》</text></text>
			</view>

			<view class="agreement-tip">本页面收集的信息仅用于注册账号</view>
			<button class="submit-btn" :disabled="!agreed" @click="handleRegister">同意并继续</button>
		</view>

	</view>
</template>

<script>
import request from '@/utils/request.js'
export default {
	data() {
		return {
			avatarUrl: '',
			nickname: '',
			phone: '',
			password: '',
			agreed: false
		};
	},
	methods: {
		// <号按钮点击事件
		handleClose() {
			// 这里可以根据需求实现关闭逻辑
			// 例如返回上一页
			uni.navigateBack({
				url: '/pages/index/index',
			});
		},
		chooseAvatar() {
			// 这里只做UI占位，实际可用uni.chooseImage实现
			uni.chooseImage({
				count: 1,
				success: res => {
					this.avatarUrl = res.tempFilePaths[0];
				}
			});
		},
		toggleAgree() {
			this.agreed = !this.agreed;
		},
		async handleRegister() {
			if (!this.nickname) {
				uni.showToast({ title: '请输入昵称', icon: 'none' });
				return;
			}
			if (!this.phone) {
				uni.showToast({ title: '请输入手机号', icon: 'none' });
				return;
			}
			const phoneReg = /^1\d{10}$/;
			if (!phoneReg.test(this.phone)) {
				uni.showToast({ title: '请输入正确的手机号', icon: 'none' });
				return;
			}
			if (!this.password) {
				uni.showToast({ title: '请输入密码', icon: 'none' });
				return;
			}
			// 保存注册数据
			uni.setStorageSync('regPhone', this.phone);
			uni.setStorageSync('regPassword', this.password);
			console.log(this.avatarUrl)
			await request({
				url: '/auth/register',
				method: 'POST',
				data: {
					username: this.nickname,
					phone: this.phone,
					password: this.password,
					avatarUrl: this.avatarUrl
				}
			}).then(res => {
				console.log(res)
			})
			// 跳转到登录页面并传递手机号
			uni.navigateTo({
				url: '/pages/login/login?phone=' + this.phone
			});
		}
	}
};
</script>

<style scoped>
.container {
	padding: 44px 20px 0 20px;
	min-height: 100vh;
	background: #fff;
	box-sizing: border-box;
}


/* X号按钮样式 */
.close-btn {
	width: 30px;
	height: 30px;
	display: flex;
	align-items: center;
	justify-content: center;
}

.x-icon {
	font-size: 24px;
	color: #333333;
	line-height: 1;
}

.title {
	text-align: center;
	font-size: 22px;
	font-weight: 500;
	color: #222;
	margin-bottom: 24px;
}

.avatar-box {
	width: 80px;
	height: 80px;
	margin: 0 auto 24px auto;
	border-radius: 8px;
	background: #f5f5f5;
	display: flex;
	align-items: center;
	justify-content: center;
	overflow: hidden;
	position: relative;
}

.avatar-img {
	width: 100%;
	height: 100%;
	object-fit: cover;
}

.avatar-placeholder {
	width: 100%;
	height: 100%;
	display: flex;
	align-items: center;
	justify-content: center;
}

.camera-icon {
	font-size: 32px;
	color: #bbb;
}

.form-box {
	background: #fff;
	border-radius: 8px;
	margin-bottom: 24px;
	box-shadow: none;
}

.form-row {
	display: flex;
	align-items: center;
	height: 48px;
	border-bottom: 1px solid #f2f2f2;
}



.label {
	width: 80px;
	color: #222;
	font-size: 15px;
	font-weight: 500;
	text-align: left;
}

.country {
	flex: 1;
	color: #888;
	font-size: 15px;
	margin-left: 2px;

}

.country-code {
	color: #888;
	font-size: 14px;
	margin-left: 2px;
}

.input {
	flex: 1;
	border: none;
	outline: none;
	background: transparent;
	font-size: 15px;
	color: #222;
	padding: 0;
	text-align: left;
}

.input-placeholder {
	color: #bbb;
	font-size: 15px;
	margin-left: 30px;
}

.agreement-row {
	display: flex;
	align-items: center;
	margin: 24px 0 0 0;
}

.checkbox {
	border-radius: 50%;
	margin-right: 8px;
	transform: scale(0.9);
}

.agreement-text {
	color: #888;
	font-size: 14px;
}

.agreement-link {
	color: #4a90e2;
}

.agreement-tip {
	color: #bbb;
	font-size: 13px;
	margin: 0px 0 24px 35px;
}

.submit-btn {
	width: 60%;
	background: #ededed;
	color: #bbb;
	font-size: 17px;
	border: none;
	border-radius: 4px;
	height: 44px;
	line-height: 44px;
	text-align: center;
	margin-bottom: 32px;
}

.submit-btn:enabled {
	background: #1aad19;
	color: #fff;
}

.button-row {
	margin-top: 330rpx;
}
</style>
